/*
 * Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.martinimix.dao.payment;

import net.martinimix.beans.bind.BindingService;
import net.martinimix.beans.bind.BindingServiceAware;
import net.martinimix.dao.DataAccessException;
import net.martinimix.domain.payment.CreditCardTransaction;
import net.martinimix.domain.payment.Payment;
import net.martinimix.domain.payment.PaymentTransaction;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bluemartini.database.BMDatabaseManager;
import com.bluemartini.database.DBUtil;
import com.bluemartini.dna.BMException;
import com.bluemartini.dna.BusinessObject;

/**
 * Provides a Blue Martini implementation of a payment data access object.
 * 
 * @author Scott Rossillo
 *
 */
public final class BlueMartiniPaymentDao implements PaymentDao, BindingServiceAware {
	
	private final static Log log = LogFactory.getLog(BlueMartiniPaymentDao.class);
	private final static String CREDIT_CARD_TXN_BIZOBJ_NAME = "CREDIT_CARD_TXN";
	private final static String PAYMENT_TXN_BIZOBJ_NAME = "PAYMENT_TXN";
	
	private BindingService bindingService;
	
	/**
	 * Creates a new payment data access object.
	 */
	public BlueMartiniPaymentDao() { }
	

	/* (non-Javadoc)
	 * @see net.martinimix.dao.bind.BindingServiceAware#setBindingService(net.martinimix.dao.bind.BindingService)
	 */
	public void setBindingService(BindingService bindingService) {
		this.bindingService = bindingService;
	}

	/* (non-Javadoc)
	 * @see net.martinimix.dao.payment.PaymentDao#saveCreditCardTransaction(net.martinimix.domain.payment.CreditCardTransaction)
	 */
	public void saveCreditCardTransaction(CreditCardTransaction transaction) {
		
		BusinessObject boTxn = bindingService.bindBusinessObject(transaction, CREDIT_CARD_TXN_BIZOBJ_NAME);
		
		if(log.isDebugEnabled()) {
			log.debug("Saving [" + CREDIT_CARD_TXN_BIZOBJ_NAME + "]: " + boTxn);
		}
		
		BMDatabaseManager.pushCurrentDatabase("store");
		try {
			DBUtil.insertBusinessObject(boTxn);
		} catch (BMException e) {
			throw new DataAccessException("Unable to save [CreditCardTransaction]!", e);
		} finally {
			BMDatabaseManager.popCurrentDatabaseQuiet("store");
		}
		
	}
	
	/**
	 * Saves the given payment as a PAYMENT business object.
	 * 
	 * @see net.martinimix.dao.payment.PaymentDao#savePayment(Payment)
	 */
	public void savePayment(Payment payment) {
		
		BusinessObject boPayment = bindingService.bindBusinessObject(payment, "PAYMENT");
		
		if(log.isDebugEnabled()) {
			log.debug("Saving PAYMENT: " + boPayment);
		}
		
		BMDatabaseManager.pushCurrentDatabase("store");
		try {
			if(payment.getId() == null) {
				DBUtil.insertBusinessObject(boPayment, true);
				payment.setId(boPayment.getLong("pay_id"));
			} else {
				DBUtil.updateBusinessObject(boPayment);
			}
		} catch (BMException e) {
			throw new DataAccessException("Unable to save [Payment]!", e);
		} finally {
			BMDatabaseManager.popCurrentDatabaseQuiet("store");
		}
		
	}
	
	/* (non-Javadoc)
	 * @see net.martinimix.dao.payment.PaymentDao#savePaymentTransaction(net.martinimix.domain.payment.PaymentTransaction)
	 */
	public void savePaymentTransaction(PaymentTransaction transaction) {
		BusinessObject boTxn = bindingService.bindBusinessObject(transaction, PAYMENT_TXN_BIZOBJ_NAME);
		
		if(log.isDebugEnabled()) {
			log.debug("Saving [" + PAYMENT_TXN_BIZOBJ_NAME + "]: " + boTxn);
		}
		
		BMDatabaseManager.pushCurrentDatabase("store");
		try {
			DBUtil.insertBusinessObject(boTxn);
		} catch (BMException e) {
			throw new DataAccessException("Unable to save [PaymentTransaction]!", e);
		} finally {
			BMDatabaseManager.popCurrentDatabaseQuiet("store");
		}
	}

}
